package cst.neu.yuejian_31.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;



import cst.neu.yuejian_31.R;

public class SettingActivity extends BaseActivity implements View.OnClickListener {
    private TextView userUpdata,clearData,help;
    private Context context;
    private TextView data;
    private Button btn_out;
    private ImageView btn_return;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        init();
        initData();


    }

    private void initData() {
        try {
            data.setText(DataCleanManager.getTotalCacheSize(getApplicationContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void init() {
        userUpdata= (TextView) findViewById(R.id.textView_userUpdata);
        btn_out= (Button) findViewById(R.id.button_out);
        btn_return= (ImageView) findViewById(R.id.imageButton_return_set);
        clearData= (TextView) findViewById(R.id.textView_clear);
        data= (TextView) findViewById(R.id.textView_data);
        help= (TextView) findViewById(R.id.textView_help);
        clearData.setOnClickListener(this);
        btn_return.setOnClickListener(this);
        btn_out.setOnClickListener(this);
        userUpdata.setOnClickListener(this);
        help.setOnClickListener(this);
    }




    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.textView_userUpdata:
                Intent intent=new Intent(SettingActivity.this,UserUpdateActivity.class);
                startActivity(intent);
                break;
            case R.id.button_out:
                sharedPreferences=getSharedPreferences("login", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.remove("user_id");
                editor.commit();
                Intent intent1=new Intent(SettingActivity.this,LoginActivity.class);
                startActivity(intent1);
                Toast.makeText(this, "注销成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.textView_clear:
                DataCleanManager.clearAllCache(getApplicationContext());
                Toast.makeText(this, "成功清除缓存", Toast.LENGTH_SHORT).show();
                try {
                    data.setText(DataCleanManager.getTotalCacheSize(getApplicationContext()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.textView_help:
                Intent intent2=new Intent(SettingActivity.this,FeedbackActivity.class);
                startActivity(intent2);
                break;
            case R.id.imageButton_return_set:
                finish();
                break;
        }


    }

}
